package easy;

public class Solution_1694 {

    public String reformatNumber(String number) {
        number = number.replace(" ", "").replace("-", "");
        char[] chars = number.toCharArray();
        StringBuffer buffer = new StringBuffer();
        int i = 0, count = 0;
        for (; i < chars.length - 2; i++) {
            if (chars[i] == ' ' || chars[i] == '-') {
                continue;
            }
            buffer.append(chars[i]);
            count++;
            if (count == 3) {
                buffer.append('-');
                count = 0;
            }
        }
        if (count == 2) {
            buffer.append('-');
        }
        buffer.append(chars[i]);
        buffer.append(chars[i + 1]);
        return buffer.toString();
    }

    public static void main(String[] args) {
        Solution_1694 model = new Solution_1694();
        System.out.println(model.reformatNumber("1-23-45 6"));
        System.out.println(model.reformatNumber("123 4-567"));
        System.out.println(model.reformatNumber("123 4-5678"));
        System.out.println(model.reformatNumber("12"));
        System.out.println(model.reformatNumber("--17-5 229 35-39475 "));
    }
}
